package com.farmcode.plugin.objectinfo.datasource;


import java.sql.*;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author oscar
 */
public class FARMCodeDatabaseMetadata {

    private Connection connection;
    private static final Logger logger = Logger.getLogger(FARMCodeDatabaseMetadata.class.getName());

    public FARMCodeDatabaseMetadata() {
    }

    public FARMCodeDatabaseMetadata(Connection connection) {
        this.connection = connection;
    }

    public ResultSet getObjects(String datasource, String[] types) throws SQLException{
        return getObject("get_objects", datasource, "%%", types);
    }
    
    public ResultSet getObject(String datasource, String tableNamePattern, String[] types) throws SQLException {
        return getObject("get_objects", datasource,"%"+ tableNamePattern +"%", types);
    }
    

    public ResultSet getObjectChilds(String datasource, String tableNamePattern, String[] types) throws SQLException {
        return getObject("get_childs", datasource, tableNamePattern, types);
    }
    
    private ResultSet getObject(String key, String schema, String tableNamePattern, String[] types) throws SQLException {
        FARMCodeResulset databaseResultset = new FARMCodeResulset();
//        logger.log(Level.INFO, MetadataUtils.getStringBundle("metadata-query", key));
        for (String type : types) {
            
            String query = MetadataUtils.getStringBundle("metadata-query", key);

            query = String.format(query, schema , type,  tableNamePattern);
//            logger.log(Level.INFO, query);
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                HashMap<String, String> result = new HashMap<String, String>();
                result.put("DATASOURCE", resultSet.getString("DATASOURCE"));
                result.put("OBJECT_NAME", resultSet.getString("OBJECT_NAME"));
                result.put("OBJECT_TYPE", resultSet.getString("OBJECT_TYPE"));
                getObjectAttributes(result, schema, tableNamePattern, resultSet.getString("OBJECT_NAME"));
                databaseResultset.resultset.add(result);
            }
           
        }
         return databaseResultset;
    }
    
      private ResultSet getObjectAttributes(HashMap<String, String> result, String schema, String object, String child) throws SQLException {
          logger.log(Level.INFO, "Attrubutes");
        FARMCodeResulset databaseResultset = new FARMCodeResulset();
        String query = MetadataUtils.getStringBundle("metadata-query", "get_attributes");

        
        query = String.format(query, schema , object, child);
         logger.log(Level.INFO, query);
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            System.out.println(resultSet.getString("VALUE"));
            System.out.println(resultSet.getString("ATTRIBUTE_NAME"));
            result.put(resultSet.getString("ATTRIBUTE_NAME"), resultSet.getString("VALUE"));
        }

        return databaseResultset;
    } 
    
}